/**
 * OSAmI: Open Source Ambient Intelligence
 * Copyright (C) 2010 UJF.
 * Contact: osami-general@osami-commons.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 */

package org.osami.gateway.mediator.drools;

import java.util.List;

import fr.liglab.adele.cilia.Data;
import fr.liglab.adele.cilia.framework.IProcessor;
import org.osami.gateway.type.Luminosity;
import org.osami.gateway.type.Temperature;
import org.osgi.service.log.LogService;
import org.ow2.jasmine.rules.api.IDroolsWorkingMemory;
import org.ow2.jasmine.rules.api.IDroolsWorkingMemoryPool;

public class ProcessorDroolsInjector implements IProcessor {

    // TODO replace with a global variable.
    private static final String GATEWAY_ID = "B1-163";

    private final String DEFAULT_MEMORY_NAME = "default";

	private final String AVERAGE_VALUE_KEY = "average-value";

	private final String AVERAGE_TYPE_KEY = "average-type";
    /**
     * Drools Service, injected by iPOJO.
     */
    private IDroolsWorkingMemory droolmem;

    /**
     * Logger, injected by iPOJO.
     */
    private LogService logger;

    /**
     * Extract type information from SenseData object and Add the appropriate object to the drools working memory.
     */
    public List process(List dataset) {
    	
         // Add the Temperature average to the Drools memory
		// Add the Temperature average to the Drools memory
    	for (Object object : dataset) {
		try {
			Data data = (Data) object;
			if (data != null) {
				String type = (String) data.getProperty(AVERAGE_TYPE_KEY);
				if (type != null) {
					Double average = (Double) data.getProperty(AVERAGE_VALUE_KEY);
					if (average != null) {
						System.out.println(type + average);
						if ("temperature".equals(type)) {
							Temperature temperature = new Temperature(average,GATEWAY_ID);
							droolmem.insert(temperature);
						}
						if ("luminosity".equals(type)) {
							Luminosity luminosity = new Luminosity(average,GATEWAY_ID);
							droolmem.insert(luminosity);
						}
						logger.log(LogService.LOG_INFO,"Average temperature has been send to drools: "+ data.getType()+ " collected, average: " + average);
						System.out.println("inserted" + average);
					} else {
						logger.log(LogService.LOG_WARNING,"Average info is null, ignoring data");
					}
				} else {
					logger.log(LogService.LOG_WARNING,"Type info is null, ignoring data");
				}
			} else {
				logger.log(LogService.LOG_WARNING,"Data gathered by sender is null");
			}
            } catch (Exception e) {
                logger.log(LogService.LOG_ERROR, "An exception has been reached while adding the temperature average to the drools memory.", e);
            }
    }
            return null;
    }

    private void bindDroolsMemoryPool(IDroolsWorkingMemoryPool pool) {
        droolmem = pool.get(DEFAULT_MEMORY_NAME);

        if (droolmem == null) {
            throw new RuntimeException("The DroolsWorkingMemory " + DEFAULT_MEMORY_NAME + "cannot be found ! mediator down ");
        }
    }

    private void stop() {
        droolmem = null;
    }

}
